import {
  getNotificationApi,
  getCmcEventListApi,
  getFmcEventListApi,
  getTemplateListByParamsApi,
  getMaintenanceApi,
  getAtaListAllApi,
  getAirportDetailApi,
  deleteItemsApi,
  submitEmailApi,
  getEmailTemplateApi
} from '@/services/emailNotification.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    // 按钮加载状态
    fdeBtnLoading: false,
    NotificationList: [],
    NotificationLoading: false,
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    CmcEventList: [], // CMC信息
    FmcEventList: [], // FMC信息
    FmcEventList2: [],
    templateList: [],
    snapshopList: [],
    AcmsList: [],
    AhmList: [],
    StarList: [],
    FDEsList: [],
    MMOList: [],
    SMTList: [],
    MSGList: [],
    AlertList: [],
    departureAirport: [], // 起飞机场列表
    aerodromeAirport: [], // 降落机场列表
    emailTemplate: [],
    addEmailList: [],

    // ATA
    ataList: [],
    ataListLoading: false,

    notificationService: {
      // 需要提交到后台的数据
      // 邮件标头模块
      subject: '',
      enableEmail: 1,
      emails: [],
      emailTemplateId: '',
      enabled: 1,
      name: '',

      // 航班模块
      airlineIds: [], // 航空公司
      fleetIds: [], // 机队列表
      modelIds: [], // 模型列表
      tailnumberIds: [], // 机号列表
      // report模块
      fmc: 'None',
      fmcNot: 'Any combination of FMC events can exist in flight leg',
      cmc: 'None',
      // acmsList: [],
      // ahmList: [],
      snapshotList: [],
      starList: [],
      // faultItem 模块
      acmsAction: 'AND',
      starAction: 'AND',
      ahmAction: 'AND',
      snapshotAction: 'AND',
      // ATA模块
      fdeAction: 'AND',
      fdeList: [],
      mmoAction: 'AND',
      mmoList: [],
      mmsgAction: 'AND',
      mmsgList: [],
      smtAction: 'AND',
      smtList: [],
      alertAction: 'AND',
      alertList: [],
      ataTitles: [],
      // 起飞机场&降落机场
      departureAirportIds: [],
      arrivalAirportIds: []
    },
    notificationFaultltem: {
      fdeEnable: 1,
      fdeEtops: 1,
      fdeDetectedIn: [],
      fdeActivity: [],
      fdePriority: '',
      mmoEnable: 1,
      mmoPriority: '',
      mmoActivity: [],
      mmoDetectedIn: [],
      smtEnable: 1,
      smtPriority: '',
      smtActivity: [],
      smtDetectedIn: [],
      cmmsgEnable: 1,
      cmmsgPriority: '',
      cmmsgDetectedIn: [],
      cmmsgActivityParentFault: [],
      cmmsgActivity: [],
      nonCorrNuisanceEnable: 1,
      nonCorrNuisanceActivity: [],
      nonCorrNuisanceDetectedIn: [],
      nonCorrNuisancePriority: '',
      alertSystemEnable: 1,
      alertSystemDetectedIn: [],
      alertSystemActivity: [],
      alertSystemPriority: ''

    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    async getNotificationList({ commit, state }, params) {
      commit('setState', { key: 'NotificationLoading', payload: true });
      const { pageNum, pageSize } = state.pagination;
      const paramsMerge = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getNotificationApi(paramsMerge);
      commit('setState', { key: 'NotificationList', payload: rep.data.list });
      commit('setState', { key: 'pagination', payload: { ...state.pagination, total: rep.data.total }});
      commit('setState', { key: 'NotificationLoading', payload: false });
    },
    async getCmcEventList({ commit, state }, params) {
      commit('setState', { key: 'NotificationLoading', payload: true });
      const rep = await getCmcEventListApi(params);
      switch (params.type) {
        case 1:
          commit('setState', { key: 'FmcEventList', payload: rep.data });
          commit('setState', { key: 'fmc', payload: rep.data[0].name });
          break;
        case 2:
          commit('setState', { key: 'FmcEventList2', payload: rep.data });
          commit('setState', { key: 'fmcNot', payload: rep.data[0].name });
          break;
        case 3:
          commit('setState', { key: 'CmcEventList', payload: rep.data });
          commit('setState', { key: 'cmc', payload: rep.data[0].name });
          break;
      }
      commit('setState', { key: 'NotificationLoading', payload: false });
    },
    async getFmcEventList({ commit, state }) {
      commit('setState', { key: 'NotificationLoading', payload: true });
      const rep = await getFmcEventListApi();
      commit('setState', { key: 'FmcEventList', payload: rep.data });
      commit('setState', { key: 'NotificationLoading', payload: false });
    },
    async getTemplateListByParams({ commit, state }, params) {
      commit('setState', { key: 'NotificationLoading', payload: true });
      const rep = await getTemplateListByParamsApi(params);
      rep.data.forEach((item) => {
        item.key = item.templateName;
        item.title = '';
      });
      switch (params.category) {
        case 'ACMS':
          commit('setState', { key: 'AcmsList', payload: rep.data });
          break;
        case 'snapshop':
          commit('setState', { key: 'snapshopList', payload: rep.data });
          break;
        case 'Star':
          commit('setState', { key: 'StarList', payload: rep.data });
          break;
        case 'AHM':
          commit('setState', { key: 'AhmList', payload: rep.data });
      }
      commit('setState', { key: 'NotificationLoading', payload: false });
    },
    async getMaintenance({ commit, state }, params) {
      commit('setState', { key: 'fdeBtnLoading', payload: true });
      const rep = await getMaintenanceApi(params);
      rep.data.list.forEach(item => {
        item.key = item.id;
        item.title = item.faultName;
      });
      switch (params.errorType[0]) {
        case 'FDE':
          commit('setState', { key: 'FDEsList', payload: rep.data.list });
          break;
        case 'MMO':
          commit('setState', { key: 'MMOList', payload: rep.data.list });
          break;
        case 'MMSG':
          commit('setState', { key: 'MSGList', payload: rep.data.list });
          break;
        case 'ALERT':
          commit('setState', { key: 'AlertList', payload: rep.data.list });
          break;
        case 'Sched. Maintenance':
          commit('setState', { key: 'SMTList', payload: rep.data.list });
          break;
      }

      commit('setState', { key: 'fdeBtnLoading', payload: false });
    },
    // 获取维修手册章节列表信息
    async getAtaListAll({ commit, state }) {
      commit('setState', { key: 'ataListLoading', payload: true });
      const rep = await getAtaListAllApi();
      rep.data.forEach(item => {
        item.key = item.name;
        if (item.name.split('-')[0] !== item.ataId) {
          item.name = item.ataId + '-' + item.name;
          item.key = item.name;
        }
      });
      commit('setState', { key: 'ataList', payload: rep.data ?? [] });
      commit('setState', { key: 'ataListLoading', payload: false });
    },

    async getAirportDetail({ commit, state }, params) {
      const serviceParams = { threeCode: params.threeCode, name: params.name };
      const rep = await getAirportDetailApi(serviceParams);
      if (!rep.data) return message.error('搜索结果为空！');
      // eslint-disable-next-line no-debugger
      if (params.statu) {
        const departureAirport = state.departureAirport;
        const hasFlag = departureAirport.some(item => item.threeCode === params.threeCode);
        if (hasFlag) {
          return message.error('不能添加相同的机场');
        }
        const infoObj = rep.data;
        const departureAirportIdsService = state.notificationService.departureAirportIds;
        const currentObj = { threeCode: infoObj.threeCode, name: infoObj.name, city: infoObj.city };
        departureAirport.push(currentObj);
        departureAirportIdsService.push(infoObj.id);
        commit('setState', { key: 'departureAirport', payload: departureAirport });
        commit('setState', { key: 'notificationService', payload: { ...state.notificationService, departureAirportIds: departureAirportIdsService }});
      } else {
        const aerodromeAirport = state.aerodromeAirport;
        const hasFlag = aerodromeAirport.some(item => item.threeCode === params.threeCode);
        if (hasFlag) {
          return message.error('不能添加相同的机场');
        }
        const infoObj = rep.data;
        const arrivalAirportIdsService = state.notificationService.arrivalAirportIds;
        const currentObj = { threeCode: infoObj.threeCode, name: infoObj.name, city: infoObj.city };
        aerodromeAirport.push(currentObj);
        arrivalAirportIdsService.push(infoObj.id);
        commit('setState', { key: 'aerodromeAirport', payload: aerodromeAirport });
        commit('setState', { key: 'notificationService', payload: { ...state.notificationService, arrivalAirportIds: arrivalAirportIdsService }});
      }
    },
    async deleteItems({ commit, state }, id) {
      try {
        const rep = await deleteItemsApi(id);
        message.success(rep.message);
        return Promise.resolve(true);
      } catch (err) {
        message.error('操作失败');
        return Promise.resolve(false);
      }
    },
    async submitEmail({ commit, state }, params) {
      const rep = await submitEmailApi(params);
      if (rep.code === 200) {
        message.success(rep.message);
        return Promise.resolve(true);
      } else {
        message.error(rep.message);
        return Promise.resolve(false);
      }
    },
    async getEmailTemplate({ commit, state }, params) {
      const rep = await getEmailTemplateApi(params);
      commit('setState', { key: 'emailTemplate', payload: rep.data });
    }

  }
};
